﻿@model IOA.MES.DataAccess.VModel.VAllocateOrder
@using IOA.MES.DataAccess.VModel;
@using IOA.MES.DataAccess.Enum;

@{
    ViewBag.Title = "编辑调拨单";
    Layout = "~/Views/Shared/_LayoutPage.cshtml";
    var warehouses = ViewBag.Warehouses as List<VWarehouse>;
}

@section Contents{
    <style>
        html {
            background-color: #FFF;
        }

        .layui-input {
            width: 300px;
        }

        .red {
            color: red;
        }
    </style>
}

<div id="page"></div>

<script type="text/template" id="tplPage">
    <form class="layui-form" style="padding: 20px 30px;">
        <div class="layui-form-item">
            <label class="layui-form-label"><i class="red">*</i>{{lang.Field.AllocateOrderNo}}:</label>
            <div class="layui-input-inline">
                <input class="layui-input" name="AllocateNo" value="@(Model?.AllocateNo)" lay-verify="required" @(Model == null ? "" : " readonly ")>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label"><i class="red">*</i>{{lang.Field.WorkorderNo}}：</label>
            <div class="layui-input-inline">
                <input class="layui-input" name="WorkOrderNo" value="@(Model?.WorkOrderNo)" lay-verify="required" @(Model == null ? "" : " readonly ") />
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label"><i class="red">*</i>{{lang.Field.FromWarehouse}}:</label>
            <div class="layui-input-inline" style="width:300px;">
                <select name="FromWarehouseCode" lay-search="">
                    @foreach (var warehouse in warehouses)
                    {
                        <option value="@warehouse.WarehouseCode" @(string.Equals(Model?.FromWarehouseCode, warehouse.WarehouseCode, StringComparison.CurrentCultureIgnoreCase) ? "selected" : "")>@warehouse.WarehouseName</option>
                    }
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label"><i class="red">*</i>{{lang.Field.ToWarehouse}}:</label>
            <div class="layui-input-inline" style="width:300px;">
                <select name="ToWarehouseCode" lay-search="">
                    @foreach (var warehouse in warehouses.Where(_ => _.WarehouseType == WarehouseType.Workshop).OrderBy(_ => _.WarehouseCode).ToList())
                    {
                        <option value="@warehouse.WarehouseCode" @(string.Equals(Model?.ToWarehouseCode, warehouse.WarehouseCode, StringComparison.CurrentCultureIgnoreCase) ? "selected" : "")>@warehouse.WarehouseName</option>
                    }
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label"><i class="red">*</i>{{lang.Field.AllocateTime}}:</label>
            <div class="layui-input-inline">
                <input id="txtAllocateTime" class="layui-input" name="AllocateTime" value="@(Model?.AllocateTime.ToString("yyyy-MM-dd HH:mm:ss"))" lay-verify="required" lay-verType="tips">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">{{lang.Field.Remark}}:</label>
            <div class="layui-input-inline">
                <input class="layui-input" name="Remark" value="@(Model?.Remark)" placeholder="请输入备注">
            </div>
        </div>

        <div class="layui-form-ite">
            <div class="layui-input-block" style="margin:0;text-align:center;">
                <button id="btnCancel" class="layui-btn" type="button" style="background-color:#CCC;">{{lang.Field.Cancel}}</button>
                <button class="layui-btn" type="button" lay-submit lay-filter="formSubmit">{{lang.Field.Submit}}</button>
            </div>
        </div>
    </form>
</script>

@section Scripts{
    <script>
        var vm = new Vue({
            el: "#page",
            template: "#tplPage",
            data: {
                lang: JSON.parse(lang).package,
            }
        });

        setTimeout(function () {
            initLayui();
        }, 50);
        function initLayui() {
            layui.use(['table', 'layer', 'form', 'laydate'], function () {
                var laydate = layui.laydate;
                var form = layui.form;

                laydate.render({
                    elem: '#txtAllocateTime',
                    type: 'datetime',
                    format: 'yyyy-MM-dd HH:mm:ss',
                    trigger: 'click',
                });

                form.on('submit(formSubmit)', function (d) {
                    $.ajax({
                        url: '/Plan/SaveAllocate',
                        data: d.field,
                        dataType: 'json',
                        success: function (res) {
                            if (res.ErrCode != 200) {
                                return layer.msg(res.ErrMsg, { icon: 5 });
                            }

                            parent.layer.close(parent.layer.getFrameIndex(window.name));
                            parent.parent.layui.index.openTabsPage("/Plan/AllocateOrderDetail?allocateNo=" + res.Data.AllocateNo, "调拨单详情【" + res.Data.AllocateNo + "】");
                        }
                    });
                });

                $('#btnCancel').on('click', function () {
                    parent.layer.close(parent.layer.getFrameIndex(window.name))
                });
            });
        }
    </script>
}
